<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增收款计划')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-receivable-add">
            <h4 class="form-header h4">基础信息</h4>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">项目：</label>
                        <div class="col-sm-8">
                            <input name="planType" class="form-control" type="hidden" value="other">
                            <input name="customerId" type="hidden" id="customerId">
                            <select name="projectId" id="projectId" class="form-control m-b" th:with="type=${@customDictService.selectDictNotStaffingProject()}" required>
                                <option value="">请选择</option>
                                <option th:each="dict : ${type}" th:text="${dict.title}" th:value="${dict.id}"></option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">项目金额：</label>
                        <div class="col-sm-8">
                            <input name="projectAmount" id="projectAmount" class="form-control" type="text" disabled>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">成本中心：</label>
                        <div class="col-sm-8">
                            <select name="businessUnitDepartmentId" class="form-control m-b" th:with="type=${@customDictService.selectDictBusinessUnitWithDepartments()}" required>
                                <option value="">请选择</option>
                                <optgroup th:each="unit: ${type}" th:label="${unit.name}">
                                    <option th:each="department : ${unit.departments}" th:text="${department.name}" th:value="${department.id}"></option>
                                </optgroup>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">收款负责人：</label>
                        <div class="col-sm-8">
                            <select name="receiverId"  class="form-control m-b" th:with="type=${@customDictService.selectUserList()}">
                                <option value="0">无</option>
                                <option th:each="dict : ${type}" th:text="${dict.userName}" th:value="${dict.userId}"></option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>

            <h4 class="form-header h4">计划明细</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addColumn()"><i class="fa fa-plus"> 增加</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="sub.delColumn()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <script th:src="@{/js/jquery.tmpl.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "erp/receivable"
        $("#form-receivable-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-receivable-add').serialize());
            }
        }

        $("input[name='planReceiveDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='latestReceiveDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='cognizanceDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(function (){
            var data = [{
                planNumber: 1,
                planReceiveDate: '',
                expectAmount: 0,
                description: '',
                rate: 30
            },{
                planNumber: 2,
                planReceiveDate: '',
                expectAmount: 0,
                description: '',
                rate: 30
            },{
                planNumber: 3,
                planReceiveDate: '',
                expectAmount: 0,
                description: '',
                rate: 30
            },{
                planNumber: 1000,
                planReceiveDate: '',
                expectAmount: 0,
                description: '',
                rate: 10
            }];
            var options = {
                data: data,
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                columns: [
                    {
                        checkbox: true
                    },
                    {
                        field: 'index',
                        align: 'center',
                        title: "序号",
                        formatter: function (value, row, index) {
                            var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                            var columnId = $.common.sprintf("<input type='hidden' name='goods[%s].id' value='%s'>", index, row.id);
                            return columnIndex + $.table.serialNumber(index) + columnId;
                        }
                    },
                    {
                        field: 'planNumber',
                        align: 'center',
                        width: '124px',
                        title: '第几笔收款',
                        formatter: function(value, row, index) {
                            var data = [{ index: index, planNumber: value }];
                            return $("#PlanNumberTpl").tmpl(data).html();
                        }
                    },
                    {
                        field: 'planReceiveDate',
                        align: 'center',
                        width: '124px',
                        title: '计划收款时间日期',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control' type='text' name='plans[%s].planReceiveDate' value='%s' placeholder='yyyy-MM-dd'>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'expectAmount',
                        align: 'center',
                        width: '124px',
                        title: '收款金额',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control expectAmount' type='text' name='plans[%s].expectAmount' value='%s'>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'rate',
                        align: 'center',
                        width: '80px',
                        title: '%',
                        formatter: function(value, row, index) {
                            // let percent = value*100;
                            var html = $.common.sprintf("<input class='form-control percent' type='text' name='plans[%s].price' value='%s'>", index, (value*1).toFixed(2));
                            return html;
                        }
                    },
                    {
                        field: 'description',
                        align: 'center',
                        title: '备注',
                        formatter: function(value, row, index) {

                            var html = $.common.sprintf("<input class='form-control' type='text' name='plans[%s].description' value='%s'>", index, value);
                            return html;
                        }
                    }]
            };
            $.table.init(options);
        });

        $("#bootstrap-table").on("post-body.bs.table", function (e, args) {
            $("input[name$='date']").datetimepicker({
                format: "yyyy-mm-dd",
                minView: "month",
                autoclose: true,
                pickerPosition:'top-right'
            });
        });

        function addColumn() {
            var row = {
                planNumber: 4,
                planReceiveDate: '',
                expectAmount: 0,
                description: '',
                rate: 0
            }
            sub.addColumn(row);
        }

        //选择项目时，载入项目相关信息，并计算收款金额
        $(function (){
            $('#projectId').change(function (){
                $.post('/erp/project/info',{ id: $(this).val()},function (data){
                    $('#customerId').val(data.customerId);
                    $('#projectAmount').val(data.amount);
                    var $percent = $('.percent');
                    var $expectAmount = $('.expectAmount');
                    for(let i=0; i < $percent.length; i++){
                        let value = data.amount * $percent.eq(i).val() /100;
                        $expectAmount.eq(i).val(value.toFixed(2));
                    }
                });

            });

            $(document).on("blur",'.percent',function(){
                if (!$('#projectAmount').val()) return;
                var index = $('.percent').index($(this));
                $('.expectAmount').eq(index).val($('#projectAmount').val() * $(this).val() /100);
            });
        });
    </script>
</body>
</html>

<script id="PlanNumberTpl" type="text/x-jquery-tmpl">
<div>
<select class='form-control' name='plans[${index}].planNumber'>

    <option value="1" {{if planNumber=="1"}}selected{{/if}}>第一笔</option>
    <option value="2" {{if planNumber=="2"}}selected{{/if}}>第二笔</option>
    <option value="3" {{if planNumber=="3"}}selected{{/if}}>第三笔</option>
    <option value="4" {{if planNumber=="4"}}selected{{/if}}>第四笔</option>
    <option value="5" {{if planNumber=="5"}}selected{{/if}}>第五笔</option>
    <option value="6" {{if planNumber=="6"}}selected{{/if}}>第六笔</option>
    <option value="7" {{if planNumber=="7"}}selected{{/if}}>第七笔</option>
    <option value="1000" {{if planNumber=="1000"}}selected{{/if}}>第末笔</option>
</select>
</div>
</script>